package com.ljf.daily.softwareautorecord.infra.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.infra.repository.impl.BasisRepositoryImpl;
import com.ljf.common.utils.CollectionUtil;
import com.ljf.common.utils.EnumUtil;
import com.ljf.daily.softwareautorecord.domain.repository.DailyDatSoftwareAutoRecordRepository;
import com.ljf.daily.softwareautorecord.dto.query.DailyDatSoftwareAutoRecordQuery;
import com.ljf.daily.softwareautorecord.enums.DailyDatSoftwareAutoRecordAutoExecutionEnum;
import com.ljf.daily.softwareautorecord.enums.DailyDatSoftwareAutoRecordModeTypeEnum;
import com.ljf.daily.softwareautorecord.infra.mapper.DailyDatSoftwareAutoRecordMapper;
import com.ljf.daily.softwareautorecord.model.DailyDatSoftwareAutoRecord;
import com.ljf.daily.softwareautorecord.vo.DailyDatSoftwareAutoRecordVO;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 赚钱软件自动记录  数据访问接口实现
 *
 * @author LJF
 * @since 2024-12-14
 */
@Component
public class DailyDatSoftwareAutoRecordRepositoryImpl extends BasisRepositoryImpl<DailyDatSoftwareAutoRecordMapper, DailyDatSoftwareAutoRecord, DailyDatSoftwareAutoRecordQuery, DailyDatSoftwareAutoRecordVO> implements DailyDatSoftwareAutoRecordRepository {

    @Override
    protected QueryWrapper<DailyDatSoftwareAutoRecord> fillWrapper(DailyDatSoftwareAutoRecordQuery query, boolean sort) throws ServiceException {
        QueryWrapper<DailyDatSoftwareAutoRecord> wrapper = super.fillWrapper(query, sort);
        if (CollectionUtil.isNotEmpty(query.getCodeList())) {
            wrapper.in(DailyDatSoftwareAutoRecord.COL_CODE, query.getCodeList());
        }
        return wrapper;
    }

    @Override
    protected void fillInfo(List<DailyDatSoftwareAutoRecordVO> resultList) throws ServiceException {
        super.fillInfo(resultList);
        Map<Integer, String> autoExecutionMap = EnumUtil.getCodeToNameMap(DailyDatSoftwareAutoRecordAutoExecutionEnum.values());
        for (DailyDatSoftwareAutoRecordVO result : resultList) {
            if (Objects.equals(result.getModeType(), DailyDatSoftwareAutoRecordModeTypeEnum.DAY.getCode())) {
                result.setExecutionDescribe(DailyDatSoftwareAutoRecordModeTypeEnum.DAY.getDescribeFormat());
            }
            else if (Objects.equals(result.getModeType(), DailyDatSoftwareAutoRecordModeTypeEnum.MONTH.getCode())) {
                result.setExecutionDescribe(String.format(DailyDatSoftwareAutoRecordModeTypeEnum.MONTH.getDescribeFormat(), result.getExecutionDate()));
            }
            else if (Objects.equals(result.getModeType(), DailyDatSoftwareAutoRecordModeTypeEnum.YEAR.getCode())) {
                result.setExecutionDescribe(String.format(DailyDatSoftwareAutoRecordModeTypeEnum.YEAR.getDescribeFormat(), result.getExecutionDate() / 100, result.getExecutionDate() % 100));
            }
            else {
                result.setExecutionDescribe("分页查询异常: 异常模式");
            }
            result.setAutoExecutionName(autoExecutionMap.getOrDefault(result.getAutoExecution(), ""));
        }
    }
}
